<!-- 案场目标达成统计 -->
<template>
  <div class="goal-reach-table">
    <dt-search ref="search" :xl="5" @reset="onReset"></dt-search>
    <Tabs ref="goalReachTabs" v-model="curState" :animated="false" @on-click="onChangeTab" name="goalReach">
      <TabPane label="线索目标" name="CLUETARGET" tab="goalReach"></TabPane>
      <TabPane label="来人目标" name="VISITOR" tab="goalReach"></TabPane>
      <TabPane label="认购目标" name="SUBSCRIPTION" tab="goalReach"></TabPane>
      <TabPane label="签约目标" name="CONTRACT" tab="goalReach"></TabPane>
      <TabPane label="回款目标" name="PAYMENT" tab="goalReach"></TabPane>
    </Tabs>
    <Tabs
      v-if="curState == 'SUBSCRIPTION' || curState == 'CONTRACT'"
      type="card"
      v-model="otherState"
      :animated="false"
      @on-click="onChangeOtherTab"
      name="otherTab">
      <TabPane label="套数目标" name="NUMBER" tab="otherTab"></TabPane>
      <TabPane label="金额目标" name="AMOUNT" tab="otherTab"></TabPane>
      <TabPane label="面积目标" name="AREA" tab="otherTab"></TabPane>
    </Tabs>

    <!-- 案场达成====== -->
    <div v-show="fromPage == 'case'">
      <div class="flex flex-horizontal-end" style="margin-bottom: 16px">
        <Button type="primary" style="margin-right: 8px" @click="toTarget" v-if="unlocking('0110050100')">
          {{ caseTargetStatus == '已锁定' ? '调整目标' : '补设目标' }}
        </Button>
        <Button type="success" @click="onExport" :disabled="totalFlag" v-if="unlocking('0110050200')">
          导出Excel
        </Button>
      </div>
      <goal-count ref="count" />
    </div>
    <!-- 公司达成====== -->
    <div v-show="fromPage == 'company' && downUpFlag">
      <goal-report-count ref="goalReportCount" @change="getData" />
    </div>
    <adjust-log ref="adjustLogRef" />
    <dt-grid ref="table" page :onFetch="onFetch">
      <!-- 公司达成====== -->
      <div class="operate-btn flex" slot="btn" v-show="fromPage == 'company'">
        <Button type="primary" @click="downUpFun">{{ downUpFlag ? '收起筛选' : '展开筛选' }}</Button>
        <Button
          type="success"
          :disabled="totalFlag"
          style="margin-left: 8px"
          @click="onExport()"
          v-if="unlocking('0207010801')">
          导出Excel
        </Button>
      </div>
    </dt-grid>
    <dt-loading :loading="loading"></dt-loading>
  </div>
</template>

<script>
  import goalCount from './goalCount'
  import goalReportCount from './goalReportCount.vue'
  import reachApi from '@/biz/report/targetReach'
  import goalColumn from '../opt/column'
  import searchOpt from '../opt/search'
  import adjustLog from './adjustLog.vue'
  import moment from 'moment'
  import caseMgr from '@/biz/crimeSceneList'
  import buildMgr from '@/biz/building'
  import { buildPage } from '@/router/buildPage.js'
  import store from '@/store'

  export default {
    components: {
      goalCount,
      goalReportCount,
      adjustLog
    },
    data() {
      return {
        curState: 'CLUETARGET',
        oldState: 'CLUETARGET',
        otherState: 'NUMBER',
        oldotherState: 'NUMBER',
        searchInfo: {
          year: moment(new Date()).format('YYYY'),
          month: moment(new Date()).format('MM'),
          buildingId: ''
        },
        orgId: '',
        fromPage: '', //区分案场、公司
        downUpFlag: true,
        caseTargetStatus: '',
        totalFlag: true,
        loading: false
      }
    },
    methods: {
      onReset() {
        this.searchInfo = {
          year: moment(new Date()).format('YYYY'),
          month: moment(new Date()).format('MM'),
          buildingId: ''
        }
        this.$refs.search.init(this.$refs.table, new searchOpt(this), this.searchInfo)
      },
      downUpFun() {
        this.$refs.table.fixHeight()
        this.downUpFlag = !this.downUpFlag
      },
      onChangeOtherTab(val) {
        if (val == this.oldotherState) return false
        this.$refs.goalReportCount.resetIndex() //公司达成切换tab重置count
        this.$refs.count.getCurTab(this.curState, this.otherState)
        this.oldotherState = val
        this.getData(true)
      },
      onChangeTab(val) {
        if (val == this.oldState) return false
        this.otherState = 'NUMBER'
        this.oldotherState = 'NUMBER'
        this.$refs.goalReportCount.resetIndex() //公司达成切换tab重置count
        this.$refs.count.getCurTab(this.curState, this.otherState)
        this.curState = val
        this.oldState = val
        this.$refs.table.fixHeight()
        this.getData(true)
      },
      checkTimes(row) {
        if (this.fromPage == 'case') {
          row.title = '顾问目标调整记录'
          row.type = 'user'
          row.curState = this.curState
        } else {
          row.title = '案场目标调整记录'
          row.type = 'case'
          row.curState = this.curState
        }
        this.$refs.adjustLogRef.show(row)
      },
      init(data) {
        this.searchInfo.year = this.$route.params?.year || moment(new Date()).format('YYYY')
        this.searchInfo.month = this.$route.params?.month || moment(new Date()).format('MM')
        this.orgIdData = data || null
        this.orgId = data?.orgId || ''
        this.getData(true)
      },
      getCurrTab(data) {
        this.curState = data?.curState || 'CLUETARGET'
        this.oldState = data?.curState || 'CLUETARGET'
        this.otherState = data?.otherState || 'NUMBER'
        this.oldotherState = data?.otherState || 'NUMBER'
        this.getData(true)
        this.$refs.count.getCurTab(this.curState, this.otherState)
      },
      getData(reset = false) {
        this.$refs.search.init(this.$refs.table, new searchOpt(this), this.searchInfo)
        this.$refs.table.init(this, new goalColumn(this))
        this.$refs.table.fetch(reset)
      },
      onFetch(data) {
        console.log(this.curState, '12')
        this.loading = true
        let params = {
          ...data,
          buildingId: this.fromPage == 'case' ? this.$dt.buildInfo().housesId : data.buildingId,
          type: reachApi.getTabsType(this.curState)
        }
        if (this.fromPage == 'company') {
          params.orgId = this.orgId
          params.bool1 = this.$refs.goalReportCount.checkIndex == 0
          params.bool2 = this.$refs.goalReportCount.checkIndex == 1
          params.bool3 = this.$refs.goalReportCount.checkIndex == 2
        }
        if (this.curState == 'CONTRACT' || this.curState == 'SUBSCRIPTION') {
          params.unitType = reachApi.getUnitType(this.otherState)
        }
        this.filterParams = { ...params, rows: 9999 }
        return reachApi
          .getTargetSettingQuery(params, this.fromPage, this.curState)
          .then((res) => {
            this.loading = false
            if (this.fromPage == 'case') {
              let countData = res.clueTargetSettingHeadResp
              this.$refs.count.init(countData)
              this.totalFlag = res.clueTargetSettingUserResp.length == 0 ? true : false
              this.caseTargetStatus = res.clueTargetSettingHeadResp?.state
              return res.clueTargetSettingUserResp
            } else {
              if (this.curState == 'CLUETARGET' || this.curState == 'VISITOR') {
                let countData = res.targetsCountInfo
                this.$refs.goalReportCount.init(countData)
                this.totalFlag = res.pageInfo.list.length == 0 ? true : false
                return res.pageInfo
              } else {
                this.getOrderCount(params)
                this.totalFlag = res.list.length == 0 ? true : false
                return res
              }
            }
          })
          .catch(() => {
            this.totalFlag = true
            this.loading = false
          })
      },
      //认购签约回款count
      getOrderCount(data) {
        reachApi.orderCount(data).then((res) => {
          this.$refs.goalReportCount.init(res)
        })
      },
      toTarget() {
        this.$router.push({
          name: 'buildingConfigure',
          params: {
            fromCustomer: 'targetManagement',
            currentTab: this.curState
          }
        })
      },
      onExport() {
        return reachApi.exportSheet(this.filterParams, this.fromPage, this.curState)
      },
      checkDetail(row) {
        let params = {
          housesId: row.buildingId
        }
        //获取案场基本信息并保存到session
        caseMgr.buildinghousesList(params).then((res) => {
          let item = { ...res.list[0] }
          // 更新案场
          buildMgr.updateBuildInfo(item)
          this.$tool.setStorage({ caseBuildingId: JSON.stringify(item.housesId) })
          store.dispatch('setRouterList', buildPage)
          // 判断案场目标达成统计跳转
          this.$router.push({
            name: 'goalReach',
            params: {
              curState: this.curState,
              otherState: this.otherState,
              year: this.searchInfo.year,
              month: this.searchInfo.month
            }
          })
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  ::v-deep .ivu-date-picker-header {
    display: none;
  }
</style>
